﻿using commonImportPlugin.Config.Else;
using commonImportPlugin.Configs.Base;
using commonImportPlugin.Configs.Other;
using commonImportPlugin.Configs.MedicalFile;
using commonImportPlugin.Configs.PbocFile;
using commonImportPlugin.Configs.SsseFile;
using commonImportPlugin.Configs.TrackFile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using commonImportPlugin.Configs.MatchFile;
using commonImportPlugin.Common;

namespace commonImportPlugin.Config
{
    public class ConfigCore : ConfigsBase
    {
        CommonUtil commonUtil = CommonUtil.GetInstance();//公共类处理核心
        public Dictionary<string, string> originConfigs = null;//原始配置
        string configPath = null;
        public AllFactoryNames allFactorys = null;// 所有工厂名
        public ConfigCore()
        {
            try
            {
                configPath = commonUtil.GetConfigPath();//配置文件路径
                originConfigs = ReadConfigs(configPath);//读取配置
                //初始化配置键值对
                LoadConfig(originConfigs);
                //初始化公共的
                CommonInit();
            }
            catch (Exception e)
            {           
                throw new Exception(e.Message);
            }            
        }   

        /// <summary>
        /// 获取工厂名
        /// </summary>
        public AllFactoryNames GetAllFactoryNames()
        {
            AllFactoryNames allFactoryNames = new AllFactoryNames();

            if (originConfigs == null)
                return null;

            //金融文件
            if (originConfigs.ContainsKey("PbocFileFactory"))
            {
                string PbocFileFactory = originConfigs["PbocFileFactory"].Trim();
                if (string.IsNullOrEmpty(PbocFileFactory))
                    throw new Exception("金融文件处理工厂名参数：PbocFileFactory为空");

                allFactoryNames.pbocFileFactory = PbocFileFactory;
            }

            //社保文件
            if (originConfigs.ContainsKey("SsseFileFactory"))
            {
                string SsseFileFactory = originConfigs["SsseFileFactory"].Trim();
                if (string.IsNullOrEmpty(SsseFileFactory))
                    throw new Exception("社保文件处理工厂名参数：SsseFileFactory为空");

                allFactoryNames.ssseFileFactory = SsseFileFactory;
            }

            //磁条文件
            if (originConfigs.ContainsKey("TrackFileFactory"))
            {
                string TrackFileFactory = originConfigs["TrackFileFactory"].Trim();
                if (string.IsNullOrEmpty(TrackFileFactory))
                    throw new Exception("磁条文件处理工厂名参数：TrackFileFactory为空");

                allFactoryNames.trackFileFactory = TrackFileFactory;
            }

            //医保文件
            if (originConfigs.ContainsKey("MedicalFileFactory"))
            {
                string MedicalFileFactory = originConfigs["MedicalFileFactory"].Trim();
                if (string.IsNullOrEmpty(MedicalFileFactory))
                    throw new Exception("医保文件处理工厂名参数：MedicalFileFactory为空");
                allFactoryNames.medicalFileFactory = MedicalFileFactory;
            }

            //匹配
            if (originConfigs.ContainsKey("MatchFactory"))
            {
                string MatchFactory = originConfigs["MatchFactory"].Trim();
                if (string.IsNullOrEmpty(MatchFactory))
                    throw new Exception("匹配处理工厂名参数：MatchFactory为空");
                allFactoryNames.matchFactory = MatchFactory;
            }
      
            //匹配文件
            if (originConfigs.ContainsKey("MatchFileFactory"))
            {
                string MatchFileFactory = originConfigs["MatchFileFactory"].Trim();
                if (string.IsNullOrEmpty(MatchFileFactory))
                    throw new Exception("匹配文件处理工厂名参数：MatchFileFactory为空");
                allFactoryNames.matchFileFactory = MatchFileFactory;
            }
            return allFactoryNames;
        }

        private void CommonInit()
        {
            //初始initConfig配置
            InitConfig initConfig = new InitConfig();
            initConfig.Init();

            //初始化countCheckConfig配置
            CountCheckConfig countCheckConfig = new CountCheckConfig();
            countCheckConfig.init();  
        }
    
        /// <summary>
        /// 获取所有文件扩展名
        /// </summary>
        /// <returns></returns>
        public string GetAllFileExtendsion(out Dictionary<string,string> outAllFileFilterRules)
        {
            string[] seperaters = new string[] { "[#]" };//分隔符

            string allFileExtensions = null;
            outAllFileFilterRules = new Dictionary<string,string>();

            //金融扩展名
            if (originConfigs.ContainsKey("PbocFileFilterRules"))
            {
                string[] extensions = originConfigs["PbocFileFilterRules"].Split(seperaters, StringSplitOptions.None);
                allFileExtensions += extensions[0] + "|";
                outAllFileFilterRules.Add("PbocFileFilterRules", originConfigs["PbocFileFilterRules"]);
            }

            //社保扩展名
            if (originConfigs.ContainsKey("SsseFileFilterRules"))
            {
                string[] extensions = originConfigs["SsseFileFilterRules"].Split(seperaters, StringSplitOptions.None);
                allFileExtensions += extensions[0] + "|";
                outAllFileFilterRules.Add("SsseFileFilterRules", originConfigs["SsseFileFilterRules"]);
            }

            //磁条扩展名
            if (originConfigs.ContainsKey("TrackFileFilterRules"))
            {
                string[] extensions = originConfigs["TrackFileFilterRules"].Split(seperaters, StringSplitOptions.None);
                allFileExtensions += extensions[0] + "|";
                outAllFileFilterRules.Add("TrackFileFilterRules", originConfigs["TrackFileFilterRules"]);
            }

            //医保扩展名
            if (originConfigs.ContainsKey("MedicalFileFilterRules"))
            {
                string[] extensions = originConfigs["MedicalFileFilterRules"].Split(seperaters, StringSplitOptions.None);
                allFileExtensions += extensions[0] + "|";
                outAllFileFilterRules.Add("MedicalFileFilterRules", originConfigs["MedicalFileFilterRules"]);
            }

            //匹配扩展名
            if (originConfigs.ContainsKey("MatchFileFilterRules"))
            {
                string[] extensions = originConfigs["MatchFileFilterRules"].Split(seperaters, StringSplitOptions.None);
                allFileExtensions += extensions[0] + "|";
                outAllFileFilterRules.Add("MatchFileFilterRules", originConfigs["MatchFileFilterRules"]);
            }

            if (outAllFileFilterRules.Count == 0)
                outAllFileFilterRules = null;

            return allFileExtensions.TrimEnd('|');           
        }
    }  
}
